﻿'This form collects all information about a new job, checks them and creates a new job

Imports Microsoft.Msdn.Samples.BITS

Public Class FrmAddJob

    ''' <summary>
    ''' This Event indicates,that the job has been added
    ''' </summary>
    ''' <remarks></remarks>
    Public Event JobAdded()

    Private Sub FrmAddDownload_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'Automatically creates a job name with the current date and time
        TxtJobName.Text = "Download created on " & DateTime.Now.ToLongDateString & " " & DateTime.Now.ToLongTimeString
        CmbPriority.SelectedIndex = 1 'Set Priority to "normal"
        CmbProtocol.SelectedIndex = 0 'Set protocol to HTTP
    End Sub

    Private Sub CmdAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CmdCreateJob.Click
        Dim i As Integer 'Enumerator for the files to be added
        Dim LocalFile, RemoteFile As String 'Variables to tweak the local and remote file name (for further development)
        Dim NewJob As Microsoft.Msdn.Samples.BITS.Job 'Represents the new BITS-Job

        If DataFiles.Rows.Count = 0 Then 'Is there no file in the list?
            'Critical message, that files are needed to add a job
            MsgBox("You have to add at least one file to the job!", MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, "Files missing")
            'Leave this sub
            Exit Sub
        End If 'If LViewFiles.Items.Count = 0 Then
        NewJob = Nothing 'Clear NewJob

        Try 'Tries to add a new job with the files
            NewJob = ModBITS.BITS.CreateJob(TxtJobName.Text, TxtDescription.Text) 'Creates the job
            For i = 0 To DataFiles.Rows.Count - 1 'Enumerates all files
                If DataFiles.Rows(i).Cells(0).Value <> "" And DataFiles.Rows(i).Cells(1).Value <> "" Then

                    LocalFile = DataFiles.Rows(i).Cells(1).Value  'Gets the local file name from the list
                    RemoteFile = CmbProtocol.Text & DataFiles.Rows(i).Cells(0).Value  'Gets the remote file name from the list
                    Debug.WriteLine("Job to add: [" & RemoteFile & "]->[" & LocalFile & "]")
                    NewJob.AddFile(LocalFile, RemoteFile) 'adds the file to the job
                    Debug.WriteLine("Job added!")
                End If
            Next i 'For i = 0 To LViewFiles.Items.Count - 1

            If ChkStartDownloadAfterCreation.Checked Then NewJob.ResumeJob() 'Start the job, if requested by user
            Me.Close() 'Close this form
            RaiseEvent JobAdded() 'Raise event, that job has been added
        Catch ex As System.ArgumentException 'This exception is thrown if something is wrong with the file names
            'Inform user about failed job creation
            MsgBox("An error occured while creating the job. Please check if you have entered valid remote and local names!", MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, "Error Creating Job")
            NewJob.Cancel() 'Cancel the new job
        Catch ex As Exception 'Any other exception
            'Inform user about failed job creation
            MsgBox("An error occured while creating the job.", MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, "Error Creating Job")
            NewJob.Cancel() 'Cancel the new job
        Finally
            'Nothing to do here
        End Try
    End Sub

    Private Sub CmdAddFile_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CmdAddFile.Click
        Dim Item As New ListViewItem 'Represents the new listviewitem

        If TxtLocalFile.Text = "" Then 'Is there no local file name?
            'Inform user that a local file name is needed
            MsgBox("You have to specify a local file name!", MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, "Local File Name Missing")
            Exit Sub 'Leave the sub
        End If 'If TxtLocalFile.Text = "" Then

        If TxtRemoteFile.Text = "" Then 'Is there no remote file name?
            'Inform user, that a remote file name is needed
            MsgBox("You have to specify a remote file!", MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, "Remote File Missing")
            Exit Sub 'Leave the sub
        End If 'If TxtRemoteFile.Text = "" Then


        Item.Text = CmbProtocol.Text & TxtRemoteFile.Text 'Text (first column) of the new item is the protocol + the remote file name
        Dim NewFile As String() = {TxtRemoteFile.Text, TxtLocalFile.Text}
        DataFiles.Rows.Add(NewFile)
        TxtRemoteFile.Text = "" 'Clear the remote file name textbox
        TxtLocalFile.Text = "" 'Clear the local file name textbox
        CmbProtocol.SelectedIndex = 0 'Set protocol to HTTP
    End Sub

    Private Sub MnuContextFilesDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MnuContextFilesDelete.Click
        Dim answer As MsgBoxResult 'Contains answer from user

        If DataFiles.SelectedRows.Count = 0 Then
            MsgBox("No entries selected!", MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, "Delete Entry")
            Exit Sub
        End If

        If DataFiles.Rows.Count > 0 Then 'Are there any list entries selected?
            'Ask the user if he REALLY wants to delete the entry
            answer = MsgBox("Are you sure that you want to delete the selected item?", MsgBoxStyle.Question + MsgBoxStyle.YesNo, "Delete Item?")
            If answer = MsgBoxResult.Yes Then 'Did the user say YES?
                DataFiles.Rows.RemoveAt(DataFiles.SelectedRows(0).Index)
            End If 'If answer = MsgBoxResult.Yes Then
        End If 'If LViewFiles.SelectedItems.Count > 0 Then
    End Sub

    Private Sub DataFiles_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataFiles.CellContentClick

    End Sub

    Private Sub DataFiles_CellContentDoubleClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataFiles.CellContentDoubleClick
        
    End Sub

    Private Sub CmdBrowse_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CmdBrowse.Click
        Dim SaveDialog As New SaveFileDialog
        SaveDialog.Filter = "All files (*.*)|*.*"
        If SaveDialog.ShowDialog = Windows.Forms.DialogResult.OK Then
            TxtLocalFile.Text = SaveDialog.FileName
        End If
    End Sub

    Private Sub DataFiles_CellMouseDoubleClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellMouseEventArgs) Handles DataFiles.CellMouseDoubleClick
        If e.ColumnIndex = 1 Then
            Dim SaveDialog As New SaveFileDialog
            SaveDialog.Filter = "All files (*.*)|*.*"
            If SaveDialog.ShowDialog = Windows.Forms.DialogResult.OK Then
                DataFiles.Rows(e.RowIndex).Cells(e.ColumnIndex).Value = SaveDialog.FileName
            End If
        End If
    End Sub

End Class